package es.atrium.ajax.ejemplodomcss;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ServletHolaMundo extends HttpServlet
{
	
	private static final long serialVersionUID = 1L;
	
	private static final String DIRECTORIO = "D:\\DIRECTORIO";
	
	
//TEST COMNIM
	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
			
		try{

			JSONArray jsonFicheros = new JSONArray();
			File file = new File(DIRECTORIO);
			File[] contenido = file.listFiles();
			
			for (int i = 0; i < contenido.length; i++){
				JSONObject jsonFichero = new JSONObject();
				jsonFichero.put("nombre", contenido[i].getName());
				
				if (contenido[i].isDirectory())
					jsonFichero.put("tipo", "item folder");
				else
					jsonFichero.put("tipo", "item file");
				
				jsonFicheros.put(jsonFichero);
			}
			
			System.out.println("jsonFicheros.toString(): "+jsonFicheros.toString());
			
			response.getWriter().write(jsonFicheros.toString());
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
		
		
		
		}
		
	protected void doPost( HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException {
			doGet(request, response);
		}
	}
